package com.etc.library.controller;

import com.etc.library.annotation.Auth;
import com.etc.library.pojo.Category;
import com.etc.library.service.CategoryService;
import com.etc.library.util.ResultBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //@Auth
    @GetMapping("/list")
    public ResultBody list(@RequestParam Map<String,Object> params){

        int pageNum = 1;
        int pageSize = 999;
        try {
             pageNum = Integer.parseInt(params.get("pageNum").toString());
             pageSize = Integer.parseInt(params.get("pageSize").toString());
        }catch (Exception e){

        }
        //一定要写在集合获取之前
        PageHelper.startPage(pageNum,pageSize);

        List<Category> list = categoryService.list(params);
        PageInfo info = new PageInfo(list);
        return ResultBody.success(info);
    }

    @PostMapping("/add")
    public ResultBody add(@RequestBody Category category){
        if(categoryService.add(category)){
            return ResultBody.success();
        }
        return ResultBody.error();
    }

    @PutMapping("/update")
    public ResultBody update(@RequestBody Category category){
        if(categoryService.update(category)){
            return ResultBody.success();
        }
        return ResultBody.error();
    }

    @DeleteMapping("/del/{cid}")
    public ResultBody update(@PathVariable Integer cid){
        Category category = new Category();
        category.setCategoryId(cid);
        category.setCState(1);
        if(categoryService.update(category)){
            return ResultBody.success();
        }
        return ResultBody.error();
    }
}
